package com.gzy.literature.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gzy.literature.dao.UsersDao; // 引入用户数据访问对象接口
import com.gzy.literature.entity.Users; // 引入用户实体类
import com.gzy.literature.service.UsersService; // 引入用户服务接口
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest; // 引入Spring Data的分页请求类（但此处在queryByPage方法中使用PageHelper，两者通常不同时使用）
import org.springframework.stereotype.Service; // 引入Spring的服务注解

import javax.annotation.Resource; // 引入Spring的依赖注入注解
import java.util.List;

@Service("usersService")
public class UsersServiceImpl implements UsersService {
    @Autowired
    private UsersDao usersDao;
    @Override
    public Users login(String account, String password) {
        if (account == null || password == null) {
            throw new IllegalArgumentException("Account and password must not be null");
        }
        return usersDao.login(account, password);
    }
    @Override
    public Users queryById(Integer id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("User ID must be a positive integer");
        }
        return usersDao.queryById(id);
    }
    @Override
    public PageInfo<Users> queryAll(Users users, int pageNum, int pageSize) {
        if (pageNum <= 0 || pageSize <= 0) {
            throw new IllegalArgumentException("Page number and page size must be positive integers");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Users> userList = usersDao.queryAll(users);
        return new PageInfo<>(userList);
    }
    @Override
    public Users insert(Users users) {
        if (users == null || users.getAccount() == null || users.getPassword() == null) {
            throw new IllegalArgumentException("User object, account, and password must not be null");
        }
        try {
            usersDao.insert(users);
        } catch (Exception e) {
            throw new RuntimeException("Failed to insert user", e);
        }
        return users;
    }
    @Override
    public Users update(Users users) {
        if (users == null || users.getId() == null || users.getId() <= 0 || users.getAccount() == null || users.getPassword() == null) {
            throw new IllegalArgumentException("User object, ID, account, and password must be valid");
        }
        try {
            usersDao.update(users);
        } catch (Exception e) {
            throw new RuntimeException("Failed to update user", e);
        }
        return queryById(users.getId());
    }
    @Override
    public boolean deleteById(Integer id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("User ID must be a positive integer");
        }
        try {
            int affectedRows = usersDao.deleteById(id);
            return affectedRows > 0;
        } catch (Exception e) {
            throw new RuntimeException("Failed to delete user", e);
        }
    }
    @Override
    public Users queryByUsername(String username) {
        if (username == null || username.trim().isEmpty()) {
            throw new IllegalArgumentException("Username must not be null or empty");
        }
        return usersDao.queryByUsername(username);
    }
    @Override
    public boolean isUsernameExist(String username) {
        if (username == null || username.trim().isEmpty()) {
            throw new IllegalArgumentException("Username must not be null or empty");
        }
        int count = usersDao.countByUsername(username);
        return count > 0;
    }
}